package com.yax.yapi.http;

import com.alibaba.fastjson.JSONObject;
import okhttp3.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

/**
 * 文件名称： OkHttpUtil<br/>
 * 初始作者： 【yax】 <br/>
 * 创建日期： 2019/12/5 14:44<br/>
 * 功能说明：  <br/>
 * <br/>
 * ================================================<br/>
 * 修改记录：<br/>
 * 修改作者 日期 修改内容<br/>
 * <br/>
 * ================================================<br/>
 * Copyright 中仑网络科技有限公司 2019/12/5 .All rights reserved.<br/>
 */
public class OkHttpUtil {
    private static  OkHttpClient okHttpClient = new OkHttpClient();
    private static Logger log = Logger.getLogger("OkHttpUtil");
    public static String  post(String url,String cookie, HashMap<String, String > paramsMap){
        long starttime=System.currentTimeMillis();
        FormBody.Builder formBodyBuilder = new FormBody.Builder();
        if(paramsMap!=null) {
            Set<String> keySet = paramsMap.keySet();
            for (String key : keySet) {
                String value = paramsMap.get(key);
                formBodyBuilder.add(key, value);
            }
        }
        FormBody formBody = formBodyBuilder.build();
        Request request = new Request
                .Builder()
                .post(formBody)
                .url(url).header("Cookie", cookie)
                .build();
        try (Response response = okHttpClient.newCall(request).execute()) {
            log.info("http耗时："+(System.currentTimeMillis()-starttime));
            return response.body().string();
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }

    public static String get(String url,String cookie, HashMap<String, String > paramsMap){
        long starttime=System.currentTimeMillis();
        if(paramsMap!=null&&!paramsMap.isEmpty()){
            StringBuilder sb=new StringBuilder();
            for(Map.Entry<String,String> entry:paramsMap.entrySet()){
                sb.append("&"+entry.getKey()+"="+entry.getValue());
            }
            url+="?"+sb.substring(1);
        }
        Request request = new Request.Builder().url(url).header("Cookie", cookie).get().build();
        try {
            Response response = okHttpClient.newCall(request).execute();
            String string = response.body().string();
            log.info("http耗时："+(System.currentTimeMillis()-starttime));
            return string;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    public static Map<String,String>  postForCookie(String url, HashMap<String, String > paramsMap){
        long starttime=System.currentTimeMillis();
        Map<String,String> result=new HashMap<>();
        FormBody.Builder formBodyBuilder = new FormBody.Builder();
        Set<String> keySet = paramsMap.keySet();
        for(String key:keySet) {
            String value = paramsMap.get(key);
            formBodyBuilder.add(key,value);
        }
        FormBody formBody = formBodyBuilder.build();

        Request request = new Request
                .Builder()
                .post(formBody)
                .url(url)
                .build();
        try (Response response = okHttpClient.newCall(request).execute()) {
            Headers headers = response.headers();
            HttpUrl loginUrl = request.url();
            List<Cookie> cookies = Cookie.parseAll(loginUrl, headers);
            StringBuilder cookieStr = new StringBuilder();
            for (Cookie cookieT : cookies) {
                cookieStr.append(cookieT.name()).append("=").append(cookieT.value() + ";");
            }
            result.put("Cookie",cookieStr.toString());
            result.put("response",response.body().string());
            log.info("http耗时："+(System.currentTimeMillis()-starttime));
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
    public static String  postCookie(String url,String cookie, HashMap<String, String > paramsMap){
        FormBody.Builder formBodyBuilder = new FormBody.Builder();
        Set<String> keySet = paramsMap.keySet();
        for(String key:keySet) {
            String value = paramsMap.get(key);
            formBodyBuilder.add(key,value);
        }
        FormBody formBody = formBodyBuilder.build();

        Request request = new Request
                .Builder()
                .post(formBody)
                .url(url).header("Cookie", cookie)
                .build();
        try (Response response = okHttpClient.newCall(request).execute()) {
            Headers headers = response.headers();
            HttpUrl loginUrl = request.url();
            List<Cookie> cookies = Cookie.parseAll(loginUrl, headers);
            StringBuilder cookieStr = new StringBuilder();
            for (Cookie cookieT : cookies) {
                cookieStr.append(cookieT.name()).append("=").append(cookieT.value() + ";");
            }
            return cookieStr.toString();
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }

    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    public static String postJSON(String url,String cookie, Object json) {
        long starttime=System.currentTimeMillis();
        RequestBody body = RequestBody.create(JSON, JSONObject.toJSONString(json));
        Request request = new Request.Builder()
                .url(url)
                .header("Cookie", cookie)
                .post(body)
                .build();
        try {
        Response response = okHttpClient.newCall(request).execute();
            log.info("http耗时："+(System.currentTimeMillis()-starttime));
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
    /**
     * @param url          下载连接
     * @param destFileDir  下载的文件储存目录
     * @param destFileName 下载文件名称，后面记得拼接后缀
     * @param listener     下载监听
     */

    public static void download(final String url, final String destFileDir, final String destFileName, final OnDownloadListener listener) {

        Request request = new Request.Builder()
                .url(url)
                .build();

        OkHttpClient client = new OkHttpClient();

        try {
            Response response = client.newCall(request).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //异步请求
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                // 下载失败监听回调
                listener.onDownloadFailed(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                InputStream is = null;
                byte[] buf = new byte[2048];
                int len = 0;
                FileOutputStream fos = null;

                //储存下载文件的目录
                File dir = new File(destFileDir);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                File file = new File(dir, destFileName);

                try {

                    is = response.body().byteStream();
                    long total = response.body().contentLength();
                    fos = new FileOutputStream(file);
                    long sum = 0;
                    while ((len = is.read(buf)) != -1) {
                        fos.write(buf, 0, len);
                        sum += len;
                        int progress = (int) (sum * 1.0f / total * 100);
                        //下载中更新进度条
                        listener.onDownloading(progress);
                    }
                    fos.flush();
                    //下载完成
                    listener.onDownloadSuccess(file);
                } catch (Exception e) {
                    listener.onDownloadFailed(e);
                }finally {

                    try {
                        if (is != null) {
                            is.close();
                        }
                        if (fos != null) {
                            fos.close();
                        }
                    } catch (IOException e) {

                    }

                }


            }
        });
    }
    public interface OnDownloadListener{

        /**
         * 下载成功之后的文件
         */
        void onDownloadSuccess(File file);

        /**
         * 下载进度
         */
        void onDownloading(int progress);

        /**
         * 下载异常信息
         */

        void onDownloadFailed(Exception e);
    }

}
